package com.example.device.polling

import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.common.view.view.BaseMvvmActivity
import com.example.device.R
import com.example.device.adapter.PollingAdapter
import com.example.device.databinding.ActivityPollingListBinding
import com.example.device.vm.PollingViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/device/PollingListActivity")
class PollingListActivity : BaseMvvmActivity<ActivityPollingListBinding>() {
    override fun getLayoutId(): Int = R.layout.activity_polling_list
    val pollingAdapter = PollingAdapter()
    private val pollingViewModel : PollingViewModel by viewModels()
    override fun initView() {
        super.initView()
        mDataBinding.back.setOnClickListener {
            finish()
        }
        mDataBinding.add.setOnClickListener {
            ARouter.getInstance().build("/device/PollingEditActivity").navigation()
        }
        mDataBinding.rv.layoutManager = LinearLayoutManager(this)
        mDataBinding.rv.adapter = pollingAdapter
        pollingViewModel.mSuccessLiveData.observe(this){
            pollingAdapter.data.clear()
            pollingAdapter.data.addAll(it.data)
            pollingAdapter.notifyDataSetChanged()
        }
        pollingViewModel.mErrorLiveData.observe(this){
            ToastUtils.showLong(it)
        }
    }
    override fun onResume() {
        super.onResume()
        pollingViewModel.getPolling()
    }
}